﻿using RXY_Mall.Application.AllFinance;
using RXY_Mall.Application.BankCard;
using RXY_Mall.Application.Statistics;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace RXY_Mall.Admin.Controllers
{
    public class FinanceController : BaseController
    {
        private IBankCardService _bankCardService;
        private IFinanceService _financeService;
        private IStatisticsService _statisticsService;
        public FinanceController()
        {
            _bankCardService = new BankCardService();
            _financeService = new FinanceService();
            _statisticsService = new StatisticsService();
        }
        // GET: Finance
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ShopWithdrawal()
        {
            return View();
        }
        public JsonResult ShopWithdrawalList(int iDisplayStart, int iDisplayLength, string dateMin, string dateMax, string code, string shopName, int status)
        {
            int totalCount = 0;
            var userList = _financeService.GetShopWithdrawalByPage(iDisplayStart, iDisplayLength, dateMin, dateMax, code, status, shopName, out totalCount);
            return Json(new { iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = userList }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult ShopWithdrawalDeal(int id=0)
        {
            ViewBag.id = id;
            return View();
        }
        [HttpPost]
        public async Task<JsonResult> CheckShopWithdrawal(int id, int status, string remark)
        {
            var result = await _financeService.CheckShopWithdrawal(MemberId,id, status, remark);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "保存成功。", sId = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }

        public ActionResult ShopBankCard()
        {
            return View();
        }
        public JsonResult ShopBankCardList(int iDisplayStart, int iDisplayLength, string dateMin, string dateMax, string shopName, string companyName, int status)
        {
            int totalCount = 0;
            var userList = _bankCardService.GetShopBankCardByPage(iDisplayStart, iDisplayLength, dateMin, dateMax, status, shopName,companyName, out totalCount);
            return Json(new { iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = userList }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult ShopBankCardDeal(int id=0)
        {
            var model = _bankCardService.GetBankCardInfoById(id);
            return View(model);
        }
        [HttpPost]
        public async Task<JsonResult> CheckShopBankCard(int id, int status, string remark)
        {
            var result = await _bankCardService.CheckShopBankCard(MemberId,id, status, remark);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "保存成功。", sId = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }

        public ActionResult UserWithdrawal()
        {
            return View();
        }
        public JsonResult UserWithdrawalList(int iDisplayStart, int iDisplayLength, string dateMin, string dateMax, string code, string userName, string realName, int status)
        {
            int totalCount = 0;
            var userList = _financeService.GetUserWithdrawalByPage(iDisplayStart, iDisplayLength, dateMin, dateMax, code, status, userName, realName, out totalCount);
            return Json(new { iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = userList }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult UserWithdrawalDeal(int id=0)
        {
            ViewBag.id = id;
            return View();
        }
        [HttpPost]
        public async Task<JsonResult> CheckUserWithdrawal(int id, int status, string remark, string dismiss_reason)
        {
            var result = await _financeService.CheckUserWithdrawal( MemberId,id, status, remark, dismiss_reason);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "保存成功。", sId = result.data });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        }

        public ActionResult UserVip()
        {
            ViewBag.vip_total = 0;
            ViewBag.vip_today = 0;
            var statistics = _statisticsService.GetVipStatistics();
            if (statistics == null)
            {
                ViewBag.vip_total = 0;
                ViewBag.vip_today = 0;
            }
            else
            {
                ViewBag.vip_total = statistics.vip_total;
                ViewBag.vip_today = statistics.vip_today;
            }
            return View();
        }
        public JsonResult UserVipList(int iDisplayStart, int iDisplayLength, string dateMin, string dateMax, string payNo, string userName, string realName, int type = -1)
        {
            int totalCount = 0;
            var userList = _financeService.GetUserVipByPage(iDisplayStart, iDisplayLength, dateMin, dateMax, payNo, type, userName, realName, out totalCount);
            return Json(new { iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = userList }, JsonRequestBehavior.AllowGet);
        }
    }
}